<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreUserRequest;
use App\Models\Project;
use App\Models\User;
use App\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    protected $UserService;
    public function __construct(UserService $UserService)
    {
        $this->UserService = $UserService;
    }

    public function login(Request $Request)
    {
        $Request->validate([
            'mobile' => 'required|max:255',
            'password' => 'required|min:6',
        ]);

        $user = User::with('projects')->with('role')->with('company')->where('mobile',$Request->mobile)->first();
        if(!$user){
            return response()->json(['code' => '404', 'msg' => '用户不存在']);
        }
        if(!$user->validateForPassportPasswordGrant($Request->password)){
            return response()->json(['code' => '404', 'msg' => '密码错误']); 
        }
        $token = $user->createToken('api')->accessToken;
        if($user->company_id > 0){
            if(!$user->role_id){
                $user->projects = Project::select('id','title','status')->where('company_id',$user->company_id)->orderBy('created_at','desc')->first();
            }
        }
        
        return response()->json(['code' => '200', 'msg' => '登录成功','user'=>$user,'token'=>$token]);
    }

    public function index(Request $Request) {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        
        $type = Auth::user()->company_id > 0 ? 'company' : 'admin';
        $sql = $this->UserService->get($search,$type);
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = User::where('id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    /**
     * 存储一个新用户。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreUserRequest $Request)
    {
        $POSTONLY = User::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['company_id'] = Auth::user()->company_id;
        $input['password'] = $Request->password ?? '123456';
        $data = $this->UserService->insert($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function update(StoreUserRequest $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = User::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $input['password'] = $Request->password ?? '123456';
        $data = $this->UserService->update($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function userBindProject(Request $Request)
    {
        if(!$Request->user_id){
            return response()->json(['code' => '404', 'msg' => '请选择用户']);
        }
        if(!$Request->projects){
            return response()->json(['code' => '404', 'msg' => '请选择项目']);
        }
        $data = $this->UserService->userBindProject($Request->input());
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function userBindDisclosure(Request $Request)
    {
        if(!$Request->user_id){
            return response()->json(['code' => '404', 'msg' => '请选择用户']);
        }
        if(!$Request->disclosures){
            return response()->json(['code' => '404', 'msg' => '请选择技术交底']);
        }
        $data = $this->UserService->userBindDisclosure($Request->input());
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function status(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = User::find($Request->id);
        if(!$data){
            return response()->json(['code' => '404', 'msg' => '记录不存在']);
        }
        $data = $this->UserService->status($data);
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

    public function delete(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = User::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

    public function statistics(Request $Request) {
        $Three = $this->UserService->statisticsThree($Request->project_id);
        $Overdue = $this->UserService->statisticsOverdue($Request->project_id);
        $Maintenance = $this->UserService->statisticsMaintenance($Request->project_id);
        $Examine = $this->UserService->statisticsExamine($Request->project_id);
        return response()->json(['code' => '200','Three' => $Three, 'Overdue' => $Overdue, 'Maintenance' => $Maintenance, 'Examine' => $Examine]);
    }
    

    public function userStatisticsList(Request $Request) {
        $data = $this->UserService->userStatisticsList($Request->project_id);
        return response()->json(['code' => '200', 'data' => $data]);
    }
}
